﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Forums.ForumsClient.Models;
using Microsoft.Forums.SyncService;
using System.Windows.Data;
using Microsoft.Forums.OfflineStore;
using System.Windows.Threading;
using System.Globalization;

namespace Microsoft.Forums.ForumsClient.Controllers
{
    /// <summary>
    /// Controller for Sycnh Settings dialog
    /// </summary>
    public class OptionsController
    {
        OptionsDataModel optionsSettingsDataModel;

        /// <summary>
        /// Constructor saves the OptionsDataModel and initializes the controller
        /// </summary>
        /// <param name="optionsDataModel"></param>
        public OptionsController(OptionsDataModel optionsDataModel)
        {
            DataModel = AppDataModel.Instance;
            optionsSettingsDataModel = optionsDataModel;
        }

        /// <summary>
        ///  Get the application level data Model.
        /// </summary>
        public AppDataModel DataModel
        {
            get;
            private set;
        }

        /// <summary>
        /// CanExecute delegate - Returns true
        /// </summary>
        public bool CanSaveOptionsSettings
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Execute delegate - Propagates user's settings and saves them
        /// </summary>
        public void SaveOptionsSettings()
        {
            switch(optionsSettingsDataModel.PostAutoExpireAgeCollectionView.CurrentItem.ToString())
            {
                case "Never":
                    Properties.Settings.Default.AutoExpireAge = -1;
                    break;
                case "7 Days":
                    Properties.Settings.Default.AutoExpireAge = 7;
                    break;
                case "30 Days":
                    Properties.Settings.Default.AutoExpireAge = 30;
                    break;
                case "60 Days":
                    Properties.Settings.Default.AutoExpireAge = 60;
                    break;
                case "90 Days":
                    Properties.Settings.Default.AutoExpireAge = 90;
                    break;
                default:
                    Properties.Settings.Default.AutoExpireAge = -1;
                    break;
            }

            switch (optionsSettingsDataModel.SyncFrequencyCollectionView.CurrentItem.ToString())
            {
                case "Every 5 minutes":
                    // Setting is 5 minutes expressed in seconds
                    Properties.Settings.Default.SyncFrequency = 300;
                    break;
                case "Every 10 minutes":
                    // Setting is 10 minutes expressed in seconds
                    Properties.Settings.Default.SyncFrequency = 600;
                    break;
                case "Every 15 minutes":
                    // Setting is 15 minutes expressed in seconds
                    Properties.Settings.Default.SyncFrequency = 900;
                    break;
                case "Every 30 minutes":
                    // Setting is 30 minutes expressed in seconds
                    Properties.Settings.Default.SyncFrequency = 1800;
                    break;
                case "Every 60 minutes":
                    // Setting is 60 minutes expressed in seconds
                    Properties.Settings.Default.SyncFrequency = 3600;
                    break;
                case "Once Per Day":
                    // Setting is 24 hours expressed in seconds
                    Properties.Settings.Default.SyncFrequency = 122400;
                    break;
                default:
                    Properties.Settings.Default.SyncFrequency = -1;
                    break;
            }
            Properties.Settings.Default.IsHeaderOnlyModeOn = optionsSettingsDataModel.IsHeaderOnlyModeSet;
            Properties.Settings.Default.IsAutoSyncOn = optionsSettingsDataModel.IsAutoSynchModeSet;
            // This writes the user's settings to user.config file 
            // under the user's local settings application data folder
            Properties.Settings.Default.Save();
        }
    }
}
